<template>
  <XtxDialog title="添加收货地址" v-model="isShow">
    <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <input v-model="formData.receiver" class="input" placeholder="请输入收货人" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input v-model="formData.contact" class="input" placeholder="请输入手机号" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity @change="getCityCode" placeholder="请选择所在地区" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input v-model="formData.address" class="input" placeholder="请输入详细地址" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton @click="isShow = false" type="gray" style="margin-right: 20px"
        >取消</XtxButton
      >
      <XtxButton type="primary" @click="submit">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
import { addAddress } from '@/api/order.js'
import { ref, reactive } from 'vue'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup () {
    const isShow = ref(false)
    // 弹窗打开
    const open = () => {
      isShow.value = true
    }

    // 表单数据 --- 未做校验
    const formData = reactive({
      receiver: '',
      contact: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 1,
      provinceCode: '',
      cityCode: '',
      countyCode: ''
    })

    // 获取地区编码
    const getCityCode = (data) => {
      formData.provinceCode = data.provinceCode
      formData.cityCode = data.cityCode
      formData.countyCode = data.countyCode
    }

    // 确认按钮
    const submit = async () => {
      // 发请求
      const { result } = await addAddress(formData)
      console.log('地址Id：' + result.id)
      // 提示
      Message({ type: 'success', message: '添加成功' })
      // 关闭弹窗
      isShow.value = false
      // 清空表单
      for (const key in formData) {
        formData[key] = ''
      }
      formData.isDefault = 1
    }

    return { open, isShow, formData, getCityCode, submit }
  }
}
</script>

<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
